package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.CommodityStockRecordDao;
import com.huilian.iotbox.admin.wap.service.CommodityStockRecordItemService;
import com.huilian.iotbox.admin.wap.service.CommodityStockRecordService;
import com.huilian.iotbox.admin.wap.service.DisplayRackItemService;
import com.huilian.iotbox.admin.wap.service.DisplayRackService;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.dto.CommodityStockRecordItemDto;
import com.huilian.iotbox.data.dto.DisplayRackItemDto;
import com.huilian.iotbox.data.enums.CommodityStockRecordFirstTypeEnum;
import com.huilian.iotbox.data.enums.CommodityStockRecordSecondTypeEnum;
import com.huilian.iotbox.data.enums.CommodityStockRecordStatusEnum;
import com.huilian.iotbox.data.po.CommodityStockRecord;
import com.huilian.iotbox.data.po.CommodityStockRecordItem;
import com.huilian.iotbox.data.po.DisplayRack;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.utils.HttpUtils;
import com.huilian.iotbox.data.vo.CommodityStockRecordVo;
import com.huilian.iotbox.data.vo.DisplayRackItemVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * @author 龙 路 飞
 * @CreateDate: 2023-09-01 14:36
 * @Description:
 */
@Service
public class CommodityStockRecordServiceImpl implements CommodityStockRecordService {

    @Resource
    private CommodityStockRecordDao commodityStockRecordDao;

    @Resource
    private DisplayRackService displayRackService;

    @Resource
    private DisplayRackItemService displayRackItemService;

    @Resource
    private CommodityStockRecordItemService commodityStockRecordItemService;

    /**
     * 根据条件查询
     */
    @Override
    public List<CommodityStockRecordVo> findCommodityStockRecordListByTime(CommodityStockRecordDto commodityStockRecordDto) {
        List<CommodityStockRecordVo> list = commodityStockRecordDao.findCommodityStockRecordListByTime(commodityStockRecordDto);
        return list;
    }

    /**
     * 根据条件查询SUM
     */
    @Override
    public CommodityStockRecordVo findCommodityStockRecordSumByTime(CommodityStockRecordDto commodityStockRecordDto) {
        CommodityStockRecordVo commodityStockRecordVo = commodityStockRecordDao.findCommodityStockRecordSumByTime(commodityStockRecordDto);
        return commodityStockRecordVo;
    }


    /**
     * 根据条件查询
     */
    @Override
    public PageInfo findCommodityStockRecordList(CommodityStockRecordDto commodityStockRecordDto) {
        Date date = new Date();
        String pattern = "HH";
        Locale locale = Locale.getDefault();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
        String time = simpleDateFormat.format(date);
        Integer hour = Integer.parseInt(time);
        if (hour >= 0 && hour <= 5 && commodityStockRecordDto.getDateType().compareTo(1) == 0) {
            //获取昨天的开始时间和结束时间 格式yyyy-MM-dd HH:mm:ss
            commodityStockRecordDto.setStartTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.START_DATETIME_FORMATTER));
            commodityStockRecordDto.setEndTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.END_DATETIME_FORMATTER));
        }
        PageHelper.startPage(commodityStockRecordDto.getPageNum(), commodityStockRecordDto.getPageSize(), " csri.createTime desc");
        List<CommodityStockRecordVo> list = commodityStockRecordDao.findCommodityStockRecordList(commodityStockRecordDto);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 生成备货单
     */
    @Override
    public void insertInventory(CommodityStockRecordDto commodityStockRecordDto) {
        final String outTradeNo = HttpUtils.getOutTradeNo();
        for (Integer deviceId : commodityStockRecordDto.getDeviceIdList()) {
            DisplayRack display = displayRackService.findDisplayByDeviceId(deviceId);
            CommodityStockRecord commodityStockRecord = new CommodityStockRecord();
            commodityStockRecord.setDeviceId(display.getDeviceId());
            commodityStockRecord.setMerchantStoreId(display.getMerchantStoreId());
            commodityStockRecord.setOutTradeNo(outTradeNo);
            commodityStockRecord.setUserId(commodityStockRecordDto.getUserId());
            commodityStockRecord.setFirstType(CommodityStockRecordFirstTypeEnum.INVENTORY.getCode());
            commodityStockRecord.setSecondaryType(CommodityStockRecordSecondTypeEnum.INVENTORY.getCode());
            commodityStockRecord.setDescription("生成备货单");
            commodityStockRecord.setStatus(CommodityStockRecordStatusEnum.NOT_HANDLED.getCode());
            commodityStockRecordDao.insert(commodityStockRecord);

            DisplayRackItemDto displayRackItemDto = new DisplayRackItemDto();
            displayRackItemDto.setIsDelete(false);
            displayRackItemDto.setNotBound(true);
            displayRackItemDto.setIsOutOfStock(true);
            displayRackItemDto.setDisplayRackId(display.getId());
            List<DisplayRackItemVo> DisplayRackItemList = displayRackItemService.findOutOfStockDetailsByDisplayRackId(displayRackItemDto);

            for (DisplayRackItemVo rackItemVo : DisplayRackItemList) {
                CommodityStockRecordItem addCommodityStockRecordItem = new CommodityStockRecordItem();
                addCommodityStockRecordItem.setCommodityId(rackItemVo.getCommodityId());
                addCommodityStockRecordItem.setCommodityStockRecordId(commodityStockRecord.getId());
                addCommodityStockRecordItem.setQuantity(rackItemVo.getCapacity()-rackItemVo.getStock());
                commodityStockRecordItemService.insert(addCommodityStockRecordItem);
            }
        }
    }

    @Override
    public PageInfo findInventoryByUserId(CommodityStockRecordDto commodityStockRecordDto) {
        commodityStockRecordDto.setFirstType(CommodityStockRecordFirstTypeEnum.INVENTORY.getCode());
        commodityStockRecordDto.setSecondaryType(CommodityStockRecordSecondTypeEnum.INVENTORY.getCode());
        PageHelper.startPage(commodityStockRecordDto.getPageNum(), commodityStockRecordDto.getPageSize(), " csr.createTime desc");
        List<CommodityStockRecordVo> stockRecordVoList = commodityStockRecordDao.findInventoryByUserId(commodityStockRecordDto);
        return new PageInfo<>(stockRecordVoList);
    }

    @Override
    public PageInfo findTakeStockOfOutOfStock(CommodityStockRecordDto commodityStockRecordDto) {
        commodityStockRecordDto.setFirstType(CommodityStockRecordFirstTypeEnum.INVENTORY.getCode());
        commodityStockRecordDto.setSecondaryType(CommodityStockRecordSecondTypeEnum.INVENTORY.getCode());
        PageHelper.startPage(commodityStockRecordDto.getPageNum(), commodityStockRecordDto.getPageSize(), " csr.createTime desc");
        List<CommodityStockRecordVo> stockRecordVoList = commodityStockRecordDao.findTakeStockOfOutOfStock(commodityStockRecordDto);
        return new PageInfo<>(stockRecordVoList);
    }

    @Override
    public List<CommodityStockRecordVo> findStatsCommodityStockRecordList(CommodityStockRecordDto commodityStockRecordDto) {
        return commodityStockRecordDao.findStatsCommodityStockRecordList(commodityStockRecordDto);
    }

    @Override
    @Transactional
    public Integer updateCommodityStockRecordStatus(CommodityStockRecordDto commodityStockRecordDto) {
        Integer results= commodityStockRecordDao.updateStatus(commodityStockRecordDto);
        CommodityStockRecordItemDto commodityStockRecordItemDto=new CommodityStockRecordItemDto();
        commodityStockRecordItemDto.setCommodityStockRecordId(commodityStockRecordDto.getId());
        commodityStockRecordItemDto.setStatus(commodityStockRecordDto.getStatus());
        commodityStockRecordItemService.updateStatus(commodityStockRecordItemDto);
        if (results<=0) {
            throw new RuntimeException("失败:暂无权限");
        }
        return results;
    }

    @Override
    public Integer updateCommodityStockRecordItemStatus(CommodityStockRecordDto commodityStockRecordDto) {
        CommodityStockRecordItemDto commodityStockRecordItemDto=new CommodityStockRecordItemDto();
        commodityStockRecordItemDto.setId(commodityStockRecordDto.getCommodityStockRecordItemId());
        commodityStockRecordItemDto.setStatus(commodityStockRecordDto.getStatus());
        Integer results = commodityStockRecordItemService.updateStatus(commodityStockRecordItemDto);
        Integer status=CommodityStockRecordStatusEnum.PROCESSING.getCode();
        List<CommodityStockRecordVo> stockRecordVoList = commodityStockRecordDao.findAllById(commodityStockRecordDto.getId());
        if (commodityStockRecordDto.getStatus().equals(CommodityStockRecordStatusEnum.COMPLETED.getCode())){
            boolean allStatus2 = stockRecordVoList.stream().allMatch(record -> Objects.equals(record.getStatus(), CommodityStockRecordStatusEnum.COMPLETED.getCode()));
            if (allStatus2) {
                status=CommodityStockRecordStatusEnum.COMPLETED.getCode();
            }
        }
        if (commodityStockRecordDto.getStatus().equals(CommodityStockRecordStatusEnum.HAS_BEEN_CANCELLED.getCode())){
            boolean allStatus2 = stockRecordVoList.stream().allMatch(record -> Objects.equals(record.getStatus(), CommodityStockRecordStatusEnum.HAS_BEEN_CANCELLED.getCode()));
            if (allStatus2) {
                status=CommodityStockRecordStatusEnum.HAS_BEEN_CANCELLED.getCode();
            }
        }
        commodityStockRecordDto.setStatus(status);
        commodityStockRecordDao.updateStatus(commodityStockRecordDto);
        return results;
    }

    @Override
    public Integer insert(CommodityStockRecord commodityStockRecord) {
        return commodityStockRecordDao.insert(commodityStockRecord);
    }

    @Override
    public int add(CommodityStockRecordDto commodityStockRecordDto) {
        return commodityStockRecordDao.add(commodityStockRecordDto);
    }

    @Override
    public CommodityStockRecordVo findCommodityStockRecordSumByDeviceNo(CommodityStockRecordDto commodityStockRecordDto) {
        return commodityStockRecordDao.findCommodityStockRecordSumByDeviceNo(commodityStockRecordDto);
    }
}













